<template>
  <u-form-grid :data="formGrid" ref="child"></u-form-grid>
</template>

<script>
  import values from '../../utils/values'

  export default {
    name: 'Order',
    data () {
      return {
        formGrid: {
          exportParams: ["orderNo", "payTime", "keyForLimit", "orderStatus", "refundStatus"],
          options: {
            autoLoad: true,
            version: 'v2',
            url: {
              page: "order/page"
            }
          },
          toolbar: {
            create: {
              show: false
            },
            delete: {
              show: false
            },
            groups: {
              show: true,
              export: {
                label: "导出全部",
                show: true,
                fileName: "所有订单数据",
                columns: [
                  {title: '订单编号', key: 'orderNo'},
                  {title: '车主名称', key: 'name'},
                  {title: '认证手机', key: 'mobileAudit'},
                  {title: '车主名称', key: 'name'},
                  {title: '认证手机', key: 'mobileAudit'},
                  {title: '分类名称', key: 'categoryName'},
                  {title: '分类代码', key: 'categoryCode'},
                  {title: '活动代码', key: 'activitySN'},
                  {title: '活动名称', key: 'activityName'},
                  {title: '商品名称', key: 'goodsName'},
                  {title: '唯一客户码', key: 'customerOpenId'},
                  {title: 'VIN码', key: 'customerVin'},
                  {title: '经销商', key: 'targetDealer'},
                  {title: '车型', key: 'carType'},
                  {title: '微信支付号', key: 'transactionNo'},
                  {title: '支付金额', key: 'payAmount'},
                  {title: '订单时间', key: 'orderTime'},
                  {title: '支付时间', key: 'payTime'},
                  {title: '订单状态', key: 'orderStatus'},
                  {title: '申退时间', key: 'applyRefundTime'},
                  {title: '申退理由', key: 'refundReason'},
                  {title: '退款状态', key: 'refundStatus'},
                  {title: '退款时间', key: 'refundTime'},
                  {title: '审核结果', key: 'refundAuditResult'}
                ]
              }
            },
            //关键字搜索框
            filter: {
              show: true,
              name: "keyWord",
              placeholder: "根据商品名称搜索"
            },
            superFilter: {
              show: true,
              label: "高级搜索",
              width: 500,
              columns: [
                {type: "text", label: "vin", name: "keyForLimit", icon: "plus", placeholder: "请填写vin"},
                {type: "text", label: "订单编号", name: "orderNo", icon: "plus", placeholder: "请填写订单号"},
                {
                  type: "select",
                  label: "订单状态",
                  name: "orderStatus",
                  clearable: true,
                  filterable: false,
                  multiple: false,
                  data: [{
                    value: '未支付',
                    name: '未支付'
                  }, {
                    value: '支付中',
                    name: '支付中'
                  }, {
                    value: '已支付',
                    name: '已支付'
                  }, {
                    value: '已过期',
                    name: '已过期'
                  }
                  ]
                },
                {
                  type: "select",
                  label: "退款状态",
                  name: "refundStatus",
                  clearable: true,
                  filterable: false,
                  multiple: false,
                  data: [{
                    value: '未申请',
                    name: '未申请'
                  }, {
                    value: '待审核',
                    name: '待审核'
                  }, {
                    value: '退款成功',
                    name: '退款成功'
                  }, {
                    value: '退款失败',
                    name: '退款失败'
                  }
                  ]
                },
                {type: "date", label: "支付时间开始", name: "payTimeStart", required: false},
                {type: "date", label: "支付时间结束", name: "payTimeEnd", required: false}
              ],
              submit: {theme: "success", label: "查询", icon: "plus"}
            }
          },
          tips: {title: "订单列表", content: "根据条件：订单状态、退款状态、vin码、 支付时间、订单号查询订单列表"},
          form: {
            labelWidth: 100,
            modal: {
              title: "详情"
            },
            columns: [
              {label: "ID", type: "text", name: "id", required: true},
              {label: "订单编号", type: "text", name: "orderNo", required: true},
              {label: "商品名称", type: "text", name: "goodsName", required: true},
              {label: "客户微信openId", type: "text", name: "customerOpenId", required: true},
              {label: "下订单时间", type: "text", name: "orderTime", required: true},
              {label: "支付时间", type: "text", name: "payTime", required: true},
              {label: "订单状态", type: "text", name: "orderStatus", required: true},
              {label: "退款时间", type: "text", name: "refundTime", required: true},
              {label: "退款状态", type: "text", name: "refundStatus", required: true},
              {label: "微信支付订单号", type: "text", name: "transactionNo", required: true},
              {label: "vin", type: "text", name: "keyForLimit", required: true},
              {label: "参加活动ID集合", type: "text", name: "activityFullPathIds", required: true},
              {label: "参加名称集合", type: "textarea", name: "activityFullPathName", required: true},
              {label: "活动内容", type: "textarea", name: "activityContent", required: true},
              {label: "申请退款理由", type: "textarea", name: "refundReason", required: true},
              {label: "审核结果", type: "textarea", name: "refundAuditResult", required: true}
            ]
          },
          table: {
            showCheckbox: false,
            operation: {
              primaryKey: "id",
              buttons: [{
                key: "view", show: true, "label": "详情", "theme": "text", click: (row, index, _this) => {
                  _this.optViewClick(row, index);
                }
              }, {
                key: "invalid", show: true, "label": "回滚订单", "theme": "button", click: (row) => {
                  if (row['orderStatus'] !== '未支付' && row['orderStatus'] !== '已过期') {
                    this.$Notice.warning({
                      desc: "此订单不符合无效条件，订单状态：【" + row['orderStatus'] + "】"
                    });
                    return;
                  }
                  if (!row['invalid']) {
                    this.$Notice.warning({
                      desc: "此订单未超时不得置为无效"
                    });
                    return;
                  }
                  this.invalidData(row);
                }
              },
                {key: "edit", show: false},
                {key: "delete", show: false}]
            },
            columns: [
              {title: '订单编号', key: 'orderNo'},
              {title: '分类名称', key: 'categoryName'},
              {title: '分类代码', key: 'categoryCode'},
              {title: '活动代码', key: 'activitySN'},
              {title: '活动名称', key: 'activityName'},
              {title: '商品名称', key: 'goodsName'},
              {title: '唯一客户码', key: 'customerOpenId'},
              {title: 'VIN码', key: 'customerVin'},
              {title: '经销商', key: 'targetDealer'},
              {title: '车型', key: 'carType'},
              {title: '微信支付号', key: 'transactionNo'},
              {title: '支付金额', key: 'payAmount'},
              {title: '订单时间', key: 'orderTime'},
              {title: '支付时间', key: 'payTime'},
              {title: '订单状态', key: 'orderStatus'},
              {title: '申退时间', key: 'applyRefundTime'},
              {title: '申退理由', key: 'refundReason'},
              {title: '退款状态', key: 'refundStatus'},
              {title: '退款时间', key: 'refundTime'},
              {title: '审核结果', key: 'refundAuditResult'}
            ]
          }
        }
      }
    },
    methods: {
      invalidData (row) {
        this.$axios.post("order/invalid/" + row['id']).then(response => {
          this.$Notice.success({
            title: "提交成功",
            desc: "无效状态设置成功"
          });
          this.$refs.child.$emit('reloadData');
        }).catch(response => {
          this.$Notice.error({
            title: "服务异常",
            desc: response.message
          })
          ;
        });
      }
    },
    watch: {
      //监听变量发生变化，将新的变量给查询参数。
      filterParams: {
        handler (curVal, oldVal) {
          this.flag = "all";
        },
        deep: true
      }
    }
  }

</script>
